package com.htgd.gardenexpo.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.service.DeviceService;
import com.htgd.gardenexpo.utils.HttpRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 对接甲板的接口
 */
@RestController
@RequestMapping("/api/jiaBan")
@Api(tags = "甲板API")
@Slf4j
public class JiaBanController {

    @Value("${device.line_url}")
    private String lineUrl;

    @Value("${device.tenant_id}")
    private String tenantId;

    @Value("${device.vlog_url}")
    private String vlogUrl;

    @Autowired
    private DeviceService deviceService;

    /**
     * 获取线路列表
     */
    @GetMapping("/getRoutes")
    @ApiOperation("获取赛事线路")
    public CommonResult getRoutes(){
        log.info("甲板API-获取赛事线路");
        try {
            //获取token
            String token = deviceService.deviceLogin();

            //获取线路
            if(token!=null){
                Map<String,Object> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("Authorization","Bearer "+token);
                headers.put("Tenant-Id",tenantId);
                String result = HttpRequest.sendHttpPostRequest2(lineUrl,headers);
                if(result!=null){
                    JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
                    Boolean ok = (Boolean) jsonObject.get("ok");
                    if(ok){
                        JSONArray data = jsonObject.getJSONArray("data");
                        return CommonResult.success("查询成功",data);
                    }
                }else {
                    return CommonResult.error("获取线路失败:"+"获取线路失败");
                }
            }
            return CommonResult.error("获取线路失败:"+"获取token失败");
        } catch (Exception e) {
            return CommonResult.error("获取线路失败："+e);
        }
    }


    /**
     * 获取Vlog
     */
    @GetMapping("/getVlog")
    @ApiOperation("获取Vlog")
    public CommonResult getVlog(@RequestParam(value = "taskId") String taskId,
                                @RequestParam(value = "userId") String userId,
                                @RequestParam(value = "raceType") String raceType
                                ){
        log.info("甲板API-获取Vlog");
        try {
            //获取token
            String token = deviceService.deviceLogin();

            //获取线路
            if(token!=null){
                //header
                Map<String,Object> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("Authorization","Bearer "+token);
                headers.put("Tenant-Id",tenantId);
                //param
                Map<String,Object> params = new HashMap<>();
                params.put("taskId", taskId);
                params.put("userId",userId);
                params.put("raceType",raceType);

                String result = HttpRequest.sendHttpGetRequest(vlogUrl,params,headers);
                if(result!=null){
                    JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
                    Boolean ok = (Boolean) jsonObject.get("ok");
                    if(ok){
                        JSONObject data = jsonObject.getJSONObject("data");
                        return CommonResult.success("查询成功",data);
                    }
                }else {
                    return CommonResult.error("获取Vlog失败:"+"获取Vlog失败");
                }
            }
            return CommonResult.error("获取Vlog失败:"+"获取token失败");
        } catch (Exception e) {
            return CommonResult.error("获取Vlog失败："+e);
        }
    }
}
